var score=0//目标公司得分
var enterpriseAttribute={}//目标公司详情
//目标公司关联数据
var News=[]
var dataNews=[]
var dataNewss=[]//1.News

var OFFICE=[]
var BE_IN_OFFICE=[]
var BE_IN_OFFICEs=[]//2.BE_IN_OFFICE

var BRANCH_AGENCY=[]
var AGENCY=[]
var AGENCYs=[]//3.BRANCH_AGENCY

var INVESTING=[]
var dataINVESTING=[]
var dataINVESTINGs=[]//4.INVESTING

var INVOLVE_CASE_Judgment=[]
var Judgment=[]
var Judgments=[]//5.INVOLVE_CASE_Judgment

var SHARE=[]
var dataSHARE=[]
var dataSHAREs=[]//6.SHARE

var INVOLVE_CASE_CourtAnnounce=[]
var dataINVOLVE_CASE_CourtAnnounce=[]
var dataINVOLVE_CASE_CourtAnnounces=[]//7.INVOLVE_CASE_CourtAnnounce

var LEGAL_REP=[]
var dataLEGAL_REP=[]
var dataLEGAL_REPs=[]//8.LEGAL_REP

var PRINCIPAL=[]
var dataPRINCIPAL=[]
var dataPRINCIPALs=[]//9.PRINCIPAL

const allData=function(){//清空数据
    News=[]
    dataNews=[]
    dataNewss=[]//1.News

    OFFICE=[]
    BE_IN_OFFICE=[]
    BE_IN_OFFICEs=[]//2.BE_IN_OFFICE

    BRANCH_AGENCY=[]
    AGENCY=[]
    AGENCYs=[]//3.BRANCH_AGENCY

    INVESTING=[]
    dataINVESTING=[]
    dataINVESTINGs=[]//4.INVESTING

    INVOLVE_CASE_Judgment=[]
    Judgment=[]
    Judgments=[]//5.INVOLVE_CASE_Judgment

    SHARE=[]
    dataSHARE=[]
    dataSHAREs=[]//6.SHARE

    INVOLVE_CASE_CourtAnnounce=[]
    dataINVOLVE_CASE_CourtAnnounce=[]
    dataINVOLVE_CASE_CourtAnnounces=[]//7.INVOLVE_CASE_CourtAnnounce

    LEGAL_REP=[]
    dataLEGAL_REP=[]
    dataLEGAL_REPs=[]//8.LEGAL_REP

    PRINCIPAL=[]
    dataPRINCIPAL=[]
    dataPRINCIPALs=[]//9.PRINCIPAL
}

// var url='http://127.0.0.1:5501'
var url='http://10.8.20.137:8060/demo'

// 失信风险预测
const scoreChange=function(){
    // console.log(score)
    let container=document.querySelector('#container')
    let scorecolor=document.querySelector("#number")
    let grade=document.querySelector('#grade')
    scorecolor.innerHTML=score
    if(31>score&&score>=0){
        // console.log('较低，《30，绿色')
        grade.innerHTML='较低'
        container.style.cssText="color: white;background-color:#007d65"
        scorecolor.style.color="#007d65"
    }else if(51>score&&score>30){
        // console.log('中等，50-30，黄色')
        grade.innerHTML='中等'
        container.style.cssText="color: white;background-color:#ffd400"
        scorecolor.style.color="#ffd400"
    }else if(71>score&&score>50){
        // console.log('较高，50-70，橙色')
        grade.innerHTML='较高'
        container.style.cssText="color: white;background-color:#e0861a"
        scorecolor.style.color="#e0861a"
    }else{
        // console.log('极高，》70,红色')
        grade.innerHTML='极高'
        container.style.cssText="color: white;background-color:#d71345"
        scorecolor.style.color="#d71345"
    }
}

document.querySelector('#text').addEventListener('click',()=>{
    if(document.querySelector('#container').style.display=='block'){
        document.querySelector('#container').style.display='none'
    }else{
        scoreChange()
        document.querySelector('#container').style.display='block'
    }
}
)

//获取菜单数据
var getData=function(data){
    var menu=document.querySelector("#menu")
    myhttp.get('/queryAllEnterprise?enterpriseName='+data).then((e)=>{
            var data=e.map((item)=>({
                    "name":item.enterpriseName,
                    "id":item.enterpriseId
                }))

                data.map((item)=>{
                    var li=document.createElement('li')
                    li.className='nav-item'
                    var a=document.createElement('a')
                    a.className='nav-link'
                    a.innerText=item.name
                    a.id=item.id
                    a.href='#'
                    li.appendChild(a)
                    menu.appendChild(li)
                })

                //初始化页面数据（默认显示第一条）
                // getContent(data[0].id)
                // document.querySelector('#titelName').innerHTML=data[0].name

                //选择公司
                document.querySelectorAll('li>a').forEach((el)=>{
                    document.querySelector('#container').style.display='none'//隐藏默认初始图
                    el.addEventListener('click',()=>{
                        document.querySelectorAll('li>a').forEach((el)=>{
                            el.style.cssText="background-color: rgb(248, 249, 250);"
                        })
                        document.querySelector('#titelName').innerHTML=el.innerHTML
                        el.style.cssText="background-color: rgb(163, 162, 163);"
                        allData()
                        getContent(el.id)
                        scoreChange()//修改分数
                })
                })
    })
}

//点击隐藏数据
window.onmousedown=()=>{
    document.querySelector('#item').style.display='none'
    document.querySelector('#items').style.display='none'
}

//获取目标公司全部数据
const getContent=async function(e){
    await myhttp.get('/queryEnterpriseDetailById?enterpriseId='+e)
    .then((e)=>{
        score=e.riskScore//得分
        let a=e.enterpriseRelationDetail//关系数据
        enterpriseAttribute=JSON.parse(e.enterpriseAttribute.replace(/'/g, '"'))//公司详情
        
        getGraph(JSON.parse(a.replace(/'/g, '"')))
    })
}

//菜单获取
onload=()=>{
    getData('')
}

//构建公司数据原型图
getGraph=function(e){
    //原生数据解析
    if(e.HAVE_News!=undefined&e.HAVE_News!=null)//1.HAVE_News
    {
        Object.keys(e.HAVE_News).forEach((index)=>{
            News.push(e.HAVE_News[index])
        })
    }

    if(e.BE_IN_OFFICE!=null&&e.BE_IN_OFFICE!=undefined)//2.BE_IN_OFFICE
    {
        Object.keys(e.BE_IN_OFFICE).forEach((index)=>{
            OFFICE.push(e.BE_IN_OFFICE[index])
        })
    }

    if(e.BRANCH_AGENCY!=null&&e.BRANCH_AGENCY!=undefined)//3.BRANCH_AGENCY
    {
        Object.keys(e.BRANCH_AGENCY).forEach((index)=>{
            BRANCH_AGENCY.push(e.BRANCH_AGENCY[index])
        })
    }

    if(e.INVESTING!=null&&e.INVESTING!=undefined)//4.INVESTING
    {
        Object.keys(e.INVESTING).forEach((index)=>{
            INVESTING.push(e.INVESTING[index])
        })
    }

    if(e.INVOLVE_CASE_Judgment!=null&&e.INVOLVE_CASE_Judgment!=undefined)//5.INVOLVE_CASE_Judgment
    {
        Object.keys(e.INVOLVE_CASE_Judgment).forEach((index)=>{
            INVOLVE_CASE_Judgment.push(e.INVOLVE_CASE_Judgment[index])
        })
    }
    
    if(e.SHARE!=null&&e.SHARE!=undefined)//6.SHARE
    {
        Object.keys(e.SHARE).forEach((index)=>{
            OFFICE.push(e.SHARE[index])
        })
    }

    if(e.INVOLVE_CASE_CourtAnnounce!=null&&e.INVOLVE_CASE_CourtAnnounce!=undefined)//7.INVOLVE_CASE_CourtAnnounce
    {
        Object.keys(e.INVOLVE_CASE_CourtAnnounce).forEach((index)=>{
            INVOLVE_CASE_CourtAnnounce.push(e.INVOLVE_CASE_CourtAnnounce[index])
        })
    }

    if(e.LEGAL_REP!=null&&e.LEGAL_REP!=undefined)//8.LEGAL_REP
    {
        Object.keys(e.LEGAL_REP).forEach((index)=>{
            LEGAL_REP.push(e.LEGAL_REP[index])
        })
    }

    if(e.PRINCIPAL!=null&&e.PRINCIPAL!=undefined)//9.PRINCIPAL
    {
        Object.keys(e.PRINCIPAL).forEach((index)=>{
            PRINCIPAL.push(e.PRINCIPAL[index])
        })
    }

    //元素节点创建
    dataNews=News.map((item,index)=>({//1.News
        id:'News'+index,
        // label:item.TITLE,
        icon: {
            show: true,
            img:url+ '/svg/新闻.svg', //可更换为其他图片地址
            // text: '...', 使用 iconfont
            width: 10,
            height: 10,
        },
        cluster: 'News',
        style: {
            // fill: '#steelblue',
            stroke: '#81C0C0',
            // lineWidth: 1,
        },
        label:'News',
    }))

    BE_IN_OFFICE=OFFICE.map((item,index)=>({//2.BE_IN_OFFICE
        id:'OFFICE'+index,
        icon: {
            show: true,
            img: url+ '/svg/gd.svg',
            width: 10,
            height: 10,
            },
        cluster: 'BE_IN_OFFICE',
        style: {
            stroke: '#2894FF',
        },
        label:item.NAME,
    }))

    AGENCY=BRANCH_AGENCY.map((item,index)=>({//3.BRANCH_AGENCY
        id:'AGENCY'+index,
        icon: {
            show: true,
            img:url+ '/svg/公司small.svg', 
            width: 10,
            height: 10,
            },
        cluster: 'BRANCH_AGENCY',
        style: {
            stroke: '#BEBEBE',
        },
        label:item.NAME,
    }))

    dataINVESTING=INVESTING.map((item,index)=>({//4.INVESTING
        id:'INVESTING'+index,
        icon: {
            show: true,
            img: url+ '/svg/控股公司.svg', 
            width: 10,
            height: 10,
            },
        cluster: 'INVESTING',
        style: {
            stroke: '#F75000',
        },
        label:item.NAME,
    }))

    Judgment=INVOLVE_CASE_Judgment.map((item,index)=>({//5.INVOLVE_CASE_Judgment
        id:'Judgment'+index,
        icon: {
            show: true,
            img: url+ '/svg/民事纠纷.svg', 
            width: 10,
            height: 10,
            },
        cluster: 'INVOLVE_CASE_Judgment',
        style: {
            stroke: '#949449',
        },
        label:'INVOLVE_CASE_Judgment',
    }))

    dataSHARE=SHARE.map((item,index)=>({//6.SHARE
        id:'SHARE'+index,
        icon: {
            show: true,
            img: url+ '/svg/gd.svg', 
            width: 10,
            height: 10,
            },
        cluster: 'SHARE',
        style: {
            stroke: '#B87070',
        },
        label:'SHARE',
    }))

    dataINVOLVE_CASE_CourtAnnounce=INVOLVE_CASE_CourtAnnounce.map((item,index)=>({//7.INVOLVE_CASE_CourtAnnounce
        id:'INVOLVE_CASE_CourtAnnounce'+index,
        icon: {
            show: true,
            img: url+ '/svg/民事纠纷.svg', 
            width: 10,
            height: 10,
            },
        cluster: 'INVOLVE_CASE_CourtAnnounce',
        style: {
            stroke: '#AFAF61',
        },
        label:'INVOLVE_CASE_CourtAnnounce',
    }))

    dataLEGAL_REP=LEGAL_REP.map((item,index)=>({//8.LEGAL_REP
        id:'LEGAL_REP'+index,
        icon: {
            show: true,
            img: url+ '/svg/gd.svg', 
            width: 10,
            height: 10,
            },
        cluster: 'LEGAL_REP',
        style: {
            stroke: '#6FB7B7',
        },
        label:item.NAME,
    }))

    dataPRINCIPAL=PRINCIPAL.map((item,index)=>({//9.PRINCIPAL
        id:'PRINCIPAL'+index,
        icon: {
            show: true,
            img: url+ '/svg/gd.svg', 
            width: 10,
            height: 10,
            },
        cluster: '#9999CC',
        style: {
            stroke: '#949449',
        },
        label:item.NAME,
    }))

    //关系连线创建
    dataNewss=News.map((item,index)=>({//1.News
        source: '0',
        target: 'News'+index,
    }))

    BE_IN_OFFICEs=OFFICE.map((item,index)=>({//2.BE_IN_OFFICE
        source: '0',
        target: 'OFFICE'+index,
    }))

    AGENCYs=BRANCH_AGENCY.map((item,index)=>({//3.BRANCH_AGENCY
        source: '0',
        target: 'AGENCY'+index,
    }))

    dataINVESTINGs=INVESTING.map((item,index)=>({//4.INVESTING
        source: '0',
        target: 'INVESTING'+index,
    }))

    Judgments=INVOLVE_CASE_Judgment.map((item,index)=>({//5.INVOLVE_CASE_Judgment
        source: '0',
        target: 'Judgment'+index,
    }))

    dataSHAREs=dataSHARE.map((item,index)=>({//6.SHARE
        source: '0',
        target: 'SHARE'+index,
    }))

    dataINVOLVE_CASE_CourtAnnounces=dataINVOLVE_CASE_CourtAnnounce.map((item,index)=>({//7.INVOLVE_CASE_CourtAnnounce
        source: '0',
        target: 'INVOLVE_CASE_CourtAnnounce'+index,
    }))

    dataLEGAL_REPs=dataLEGAL_REP.map((item,index)=>({//8.LEGAL_REP
        source: '0',
        target: 'LEGAL_REP'+index,
    }))

    dataPRINCIPALs=dataPRINCIPAL.map((item,index)=>({//9.PRINCIPAL
        source: '0',
        target: 'PRINCIPAL'+index,
    }))

    data.nodes=[
        {//目标公司元素
            id: '0',
            // label: enterpriseAttribute.公司名称,
            size: 25,
            cluster: 'a',
            style:{
            lineWidth: 2,
            },
            icon: {
            show: true,
            img: url+ '/svg/big.svg', //可更换为其他图片地址
            // text: '...', 使用 iconfont
            width: 16,
            height: 16,
            },
        }
    ]
    data.edges=[]//清空历史目标公司数据
    data.nodes.push.apply(data.nodes,dataNews)
    data.edges.push.apply(data.edges,dataNewss)//1.News

    data.nodes.push.apply(data.nodes,BE_IN_OFFICE)
    data.edges.push.apply(data.edges,BE_IN_OFFICEs)//2.BE_IN_OFFICE

    data.nodes.push.apply(data.nodes,AGENCY)
    data.edges.push.apply(data.edges,AGENCYs)//3.BRANCH_AGENCY

    data.nodes.push.apply(data.nodes,dataINVESTING)
    data.edges.push.apply(data.edges,dataINVESTINGs)//4.INVESTING

    data.nodes.push.apply(data.nodes,Judgment)
    data.edges.push.apply(data.edges,Judgments)//5.INVOLVE_CASE_Judgment

    data.nodes.push.apply(data.nodes,dataSHARE)
    data.edges.push.apply(data.edges,dataSHAREs)//6.SHARE

    data.nodes.push.apply(data.nodes,dataINVOLVE_CASE_CourtAnnounce)
    data.edges.push.apply(data.edges,dataINVOLVE_CASE_CourtAnnounces)//7.INVOLVE_CASE_CourtAnnounce

    data.nodes.push.apply(data.nodes,dataLEGAL_REP)
    data.edges.push.apply(data.edges,dataLEGAL_REPs)//8.LEGAL_REP

    data.nodes.push.apply(data.nodes,dataPRINCIPAL)
    data.edges.push.apply(data.edges,dataPRINCIPALs)//9.PRINCIPAL

    document.querySelector('#start').style.display='none'
    document.querySelector('#text').style.display='block'
    graph.data(data);
    graph.render();//画图
}

//搜索
document.querySelector('#select').addEventListener('click',()=>{
    let selectName= document.querySelector('#selectName').value
    document.querySelector("#menu").innerHTML = ""
    getData(selectName)
})
onkeydown=()=>{
    var event = window.event || arguments.callee.caller.arguments[0];
    if (event.keyCode == 13){//回车键
        let selectName= document.querySelector('#selectName').value
        document.querySelector("#menu").innerHTML = ""
        getData(selectName)
    }
}